package com.sky.service.impl;


import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.IAddressBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 地址簿 服务实现类
 * </p>
 *
 * @author fcy
 * @since 2025-09-12
 */
@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {
    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @Override
    public Result<String> addAddress(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        this.save(addressBook);
        log.info("添加地址成功");
        return Result.success("添加成功");
    }

    @Override
    public List<AddressBook> getList() {
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> list = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .list();
        return list;
    }
    /**
     * 获取默认地址
     * @return
     */
    @Override
    public AddressBook getDefault() {
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getIsDefault, 1)
                .one();
        return addressBook;
    }
    /**
     * 设置默认地址
     * @param id
     * @return
     */
    @Override
    public Result<String> setDefault(Integer id) {
        Long userId = BaseContext.getCurrentId();
        AddressBook one = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getIsDefault, 1)
                .one();
        if(one!=null){
            one.setIsDefault(0);
            updateById(one);
        }
        AddressBook addressBook = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getId, id)
                .one();
        addressBook.setIsDefault(1);
        updateById(addressBook);
        return Result.success("设置成功");
    }
    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @Override
    public Result<String> change(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        AddressBook one = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getId, addressBook.getId())
                .one();
        BeanUtils.copyProperties(addressBook,one);
        updateById(one);
        return Result.success("修改成功");
    }
    /**
     * 删除地址
     * @param id
     * @return
     */
    @Override
    public Result<String> deleteById(Integer id) {
        this.removeById( id);
        return Result.success("删除成功");
    }
}
